﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace WeifenLuo.WinFormsUI.Base
{
    public partial class FormBase : DockContent
    {
        protected string ms_Path = Application.StartupPath;
        public MainBaseForm mMainForm;
        public AppSecurityAttribute mFormRight;
        public ArrayList mal_nameList = new ArrayList();
        public ArrayList mal_textList = new ArrayList();

        public FormBase mfrmParentForm; // For recognizing the form that opens this form.
        public FormBase mfrmChildForm; // Save the child form that this form (parent current form) opens

        public FormBase()
        {
            InitializeComponent();
        }

        private string m_fileName = string.Empty;
        public string FileName
        {
            get { return m_fileName; }
            set
            {
                if (value != string.Empty)
                {
                    Stream s = new FileStream(value, FileMode.Open);

                    FileInfo efInfo = new FileInfo(value);

                    string fext = efInfo.Extension.ToUpper();

                    //if (fext.Equals(".RTF"))
                    //    richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText);
                    //else
                    //    richTextBox1.LoadFile(s, RichTextBoxStreamType.PlainText);
                    s.Close();
                }

                m_fileName = value;
                this.ToolTipText = value;
            }
        }

        #region Methods set enabled for Toolbar & Menu
        protected void SetNavigator(bool ab_switch)
        {
            if (mMainForm == null) return;
            mMainForm. mnu_FirstRecord.Enabled = ab_switch;		// Move First
            mMainForm.tbb_FirstRec.Enabled = ab_switch;		// Move First on Toolbar

            mMainForm.mnu_PreviousRecord.Enabled = ab_switch;	// Move Previous
            mMainForm.tbb_PreviousRec.Enabled = ab_switch;	// Move Previous on Toolbar

            mMainForm.mnu_NextRecord.Enabled = ab_switch;		// Move Next
            mMainForm.tbb_NextRec.Enabled = ab_switch;		// Move Next on Toolbar
            mMainForm.mnu_LastRecord.Enabled = ab_switch;		// Move Last
            mMainForm.tbb_LastRec.Enabled = ab_switch;		// Move Last on Toolbar
        }

        protected void SetFind(bool ab_switch)
        {
            if (mMainForm != null) mMainForm.mnu_Find.Enabled = ab_switch; // Find
        }

        protected void SetAdd(bool ab_switch)
        {
            if (mMainForm == null) return;
            mMainForm.mnu_AddNew.Enabled = ab_switch;// Add New
            mMainForm.tbb_Add.Enabled = ab_switch;// Add New on Toolbar
        }

        protected void SetDelete(bool ab_switch)
        {
            if (mMainForm == null) return;
            mMainForm.mnu_Delete.Enabled = ab_switch; // Delete
            mMainForm.tbb_Delete.Enabled = ab_switch; // Delete on Toolbar
        }

        protected void SetModify(bool ab_switch)
        {
            if (mMainForm == null) return;
            mMainForm.mnu_Modify.Enabled = ab_switch; // Modify
            mMainForm.tbb_Modify.Enabled = ab_switch; // Modify on Toolbar
            mMainForm.mnu_Find.Enabled = ab_switch;	 // Find
        }

        protected void SetValidate(bool ab_switch)
        {
            if (mMainForm == null) return;
            mMainForm.mnu_Save.Enabled = ab_switch; // Save
            mMainForm.tbb_Save.Enabled = ab_switch; // Save on Toolbar
            mMainForm.mnu_Cancel.Enabled = ab_switch; // Cancel
            mMainForm.tbb_Cancel.Enabled = ab_switch; // Cancel on Toolbar
        }

        protected void SetSave(bool ab_switch)
        {
            if (mMainForm == null) return;
            mMainForm.mnu_Save.Enabled = ab_switch; // Save
            mMainForm.tbb_Save.Enabled = ab_switch; // Save on Toolbar
        }

        protected void SetCancel(bool ab_switch)
        {
            if (mMainForm == null) return;
            mMainForm.mnu_Cancel.Enabled = ab_switch; // Cancel
            mMainForm.tbb_Cancel.Enabled = ab_switch; // Cancel on Toolbar
        }

        protected void SetDataManipulation(bool ab_switch)
        {
            if (mMainForm == null) return;
            SetNavigator(ab_switch);
            //mMainForm.mnu_PrintDir.Enabled = ab_switch;		// Print 
            mMainForm.tbb_PrintDir.Enabled = ab_switch;		// Print on Toolbar
            //mMainForm.mnu_PrintPre.Enabled = ab_switch;	    // Print Preview
            mMainForm.tbb_PrintPre.Enabled = ab_switch;	    // Print Preview on Toolbar
            mMainForm.mnu_List.Enabled = ab_switch;			// List 
            mMainForm.tbb_List.Enabled = ab_switch;			// List on Toolbar
            mMainForm.mnu_Refresh.Enabled = ab_switch;		// Refresh data 
            SetDataActions(ab_switch);
            SetValidate(ab_switch);
            SetFind(ab_switch);
        }

        protected void SetDataActions(bool ab_switch)
        {
            if (mMainForm == null) return;
            SetNavigator(ab_switch);
            mMainForm.mnu_AddNew.Enabled = ab_switch; // Add
            mMainForm.tbb_Add.Enabled = ab_switch; // Add on Toolbar
            mMainForm.mnu_Delete.Enabled = ab_switch; // Delete
            mMainForm.tbb_Delete.Enabled = ab_switch; // Delete on Toolbar
            mMainForm.mnu_Modify.Enabled = ab_switch; // Modify
            mMainForm.tbb_Modify.Enabled = ab_switch; // Modify on Toolbar
        }

        protected void SetList(bool ab_switch)
        {
            if (mMainForm == null) return;
            mMainForm.mnu_List.Enabled = ab_switch;// List
            mMainForm.tbb_List.Enabled = ab_switch;// List on Toolbar
        }

        protected void SetPrint(bool ab_switch)
        {
            if (mMainForm == null) return;
            //mMainForm.mnu_PrintDir.Enabled = ab_switch;// Print
            mMainForm.tbb_PrintDir.Enabled = ab_switch;// Print on Toolbar
        }

        protected void SetPrintPreview(bool ab_switch)
        {
            if (mMainForm == null) return;
           // mMainForm.mnu_PrintPre.Enabled = ab_switch;// Print Preview
            mMainForm.tbb_PrintPre.Enabled = ab_switch;// Print Preview on Toolbar
        }

        protected void SetRefreshData(bool ab_switch)
        {
            if (mMainForm == null) return;
            mMainForm.mnu_Refresh.Enabled = ab_switch;// Refresh data
        }

        public void SetAccessRight(bool ab_switch)
        {
            if (mMainForm == null) return;
            mMainForm.mnu_AccRight.Enabled = ab_switch;// Access Right
        }

        public void SetSecurity(bool ab_switch)
        {
            if (mMainForm == null) return;
            mMainForm.mnu_User.Enabled = ab_switch;// Security menu
            mMainForm.mnu_GroupUser.Enabled = ab_switch;// Security menu
            mMainForm.mnu_AccRight.Enabled = ab_switch;// Security menu
        }
        #endregion

        #region Virtual methods and events
        protected virtual void BasicForm_Load(object sender, System.EventArgs e)
        {
            mMainForm = (MainBaseForm)this.ParentForm;
            if (mMainForm != null)
            {
                 mFormRight = mMainForm.mUserRight.GetAccess(this.Name);
            }
            else
                // Get the main form For the Selection form or the Drill down form. 
                // Or some forms that opened in Modal mode.
                mMainForm = (MainBaseForm)this.Owner;
        }

        protected virtual void BaseForm_Shown(object sender, EventArgs e)
        { }

        protected virtual void RefreshToolbar()
        { }

        protected virtual void BaseForm_Activated(object sender, System.EventArgs e)
        {
            if (mMainForm == null) return;
            RefreshToolbar();
            mMainForm.stbUnderMouse.Text = "Ready ... ";
        }

        protected virtual void BasicForm_Closing(object sender, FormClosingEventArgs e)
        {
            if (mMainForm != null) mMainForm.RefreshToolbar();
        }

        protected virtual void btnHelp_Click(object sender, System.EventArgs e)
        {
            //Help.ShowHelp(this, ms_Path + @"\Help\ERP3S.chm", ms_keyWordforHelp);
        }

        protected virtual void BaseForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {	// Close form when the Escape key is pressed.
            if (e.KeyCode == Keys.Escape)
            {
                Close();
                return;
            }
            //if (e.KeyCode == Keys.F1 && e.Control)
            //	Help.ShowHelp(this, ms_Path + @"\Help\ERP3S.chm", ms_keyWordforHelp);
        }

        public virtual void EnterKey_SelectNextControl(object sender, System.Windows.Forms.KeyEventArgs e)
        {	// Move to next control with Enter key (the same Tab action)
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
                this.SelectNextControl(this.ActiveControl, true, true, true, true);
            }
        }

        public virtual void GetControlNames(out ArrayList al_list, out ArrayList al_listText)
        {
            al_list = mal_nameList;
            al_listText = mal_textList;
        }
        #endregion
    }
}
